package Operation;

import Dao.DataOperation;
import JavaBeans.Book;
import JavaBeans.State;

import java.util.Scanner;

/**
 * 借阅图书功能
 */
public class BorrowOperation implements IOperation{

    @Override
    public void operate() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您要借阅的图书ID:");
        int id = in.nextInt();
        String sql = "select id,state from book_list where id = ?";
        Book book = DataOperation.queryOne(Book.class, sql, id);
        if (book != null){
            if(book.getState().equals("借出")){
                System.out.println("当前图书已被借出！");
            }else{
                //修改图书状态
                sql = "update book_list set state = '借出' where id = ?";
                boolean isUpdated = DataOperation.update(sql, id);
                if (isUpdated){
                    System.out.println("借阅成功！");
                }else{
                    System.out.println("借阅失败！");
                }
            }
        }else{
            System.out.println("对不起！没有查询到您要借阅的图书。");
        }
    }
}
